﻿using System;
using Autofac;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Wslzy.Auxiliary.Infrastructure.Datas.EF.Core;
using Wslzy.Auxiliary.Infrastructure.Datas.UnitOfWorks;

namespace Wslzy.Auxiliary.Infrastructure.Datas
{
    /// <summary>
    /// 服务扩展
    /// </summary>
    public static partial class Extensions
    {
        /// <summary>
        /// 注册工作单元服务
        /// </summary>
        /// <typeparam name="TService">工作单元接口类型</typeparam>
        /// <typeparam name="TImplementation">工作单元实现类型</typeparam>
        /// <param name="services">服务集合</param>
        /// <param name="configAction">配置操作</param>
        public static IServiceCollection AddUnitOfWork<TService, TImplementation>(this IServiceCollection services, Action<DbContextOptionsBuilder> configAction)
            where TService : class, IUnitOfWork
            where TImplementation : UnitOfWorkBase, TService
        {
            services.AddDbContext<TImplementation>(configAction);
            services.TryAddScoped<TService, TImplementation>();
            return services;
        }

        /// <summary>
        /// 注册工作单元服务
        /// </summary>
        /// <typeparam name="TService">工作单元接口类型</typeparam>
        /// <typeparam name="TImplementation">工作单元实现类型</typeparam>
        /// <param name="services">服务集合</param>
        /// <param name="connection">连接字符串</param>
        /// <param name="level">Ef日志级别</param>
        public static IServiceCollection AddUnitOfWork<TService, TImplementation>(this IServiceCollection services, string connection)
            where TService : class, IUnitOfWork
            where TImplementation : UnitOfWorkBase, TService
        {
            //EfConfig.LogLevel = level;
            return AddUnitOfWork<TService, TImplementation>(services, builder => {
                ConfigConnection<TImplementation>(builder, connection);
            });
        }

        /// <summary>
        /// 配置连接字符串
        /// </summary>
        private static void ConfigConnection<TImplementation>(DbContextOptionsBuilder builder, string connection) where TImplementation : UnitOfWorkBase
        {
            var type = typeof(TImplementation).BaseType;
            if (type == typeof(Wslzy.Auxiliary.Infrastructure.Datas.EF.SqlServer.UnitOfWork))
            {
                builder.UseSqlServer(connection);
                return;
            }
            if (type == typeof(Wslzy.Auxiliary.Infrastructure.Datas.EF.MySql.UnitOfWork))
            {
                builder.UseMySql(connection);
                return;
            }
            //if (type == typeof(Wslzy.Auxiliary.Infrastructure.Datas.EF.PgSql.UnitOfWork))
            //{
            //    builder.UseNpgsql(connection);
            //    return;
            //}
        }



        public static void RegistBefore<TService, TImplementation>(ContainerBuilder builder)
            where TService : class, IUnitOfWork
            where TImplementation : UnitOfWorkBase, TService
        {
            var connection = "data source=localhost;initial catalog=Sample;integrated security=false;persist security info=True;User ID=sa;Password=root";
            // 首先注册 options，供 DbContext 服务初始化使用
            builder.Register(c =>
            {
                var optionsBuilder = new DbContextOptionsBuilder<TImplementation>();
                optionsBuilder.UseSqlServer(connection);
                return optionsBuilder.Options;
            }).InstancePerLifetimeScope();



            builder.RegisterType<UnitOfWorkManager>()
                .As<IUnitOfWorkManager>()
                .InstancePerLifetimeScope();

            // 注册 DbContext
            builder.RegisterType<TImplementation>()
                .As<TService>()
                .InstancePerLifetimeScope();

        }
    }
}
